package com.sen.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sen.constant.StockConstant;
import com.sen.entity.*;
import com.sen.service.*;
import com.sen.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 入库表表头
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2021-01-08 17:54:40
 */
@RestController
@RequestMapping("enterstock")
public class EnterstockController {
    @Autowired
    private EnterstockService enterstockService;
    @Autowired
    private EnterstockdetailService enterstockdetailService;
    @Autowired
    private StockheadService stockheadService;
    @Autowired
    private WarehouseService warehouseService;
    @Autowired
    private StockService stockService;

    //获取入库信息
    @RequestMapping("/getEnter")
    public R getEnter(@RequestParam Map<String,String> stockheadMap){

        Page<EnterstockEntity> enterstockEntityPage = enterstockService.queryAll(stockheadMap);
        return R.ok().put("enter",enterstockEntityPage);
//        return R.ok();
    }

    /**
     * 入库通过 添加入库信息，增加仓库数量，修改订单装填
     * @param stockheadData
     * @param eid
     * @return
     */
    @PostMapping("/wareYes")
    public R wareYes(@RequestParam String stockheadData,@RequestParam Integer eid){
        StockheadEntity stockheadEntity = JSON.parseObject(stockheadData, StockheadEntity.class);
        System.out.println(eid+"-----------------"+stockheadEntity);
        //添加入库信息
        //获取采购总的数量
        List<StockEntity> stockEntityList = stockService.list(new QueryWrapper<StockEntity>().eq("hno", stockheadEntity.getHno()).select("count"));
        Integer stockCount = 0;
        for (StockEntity stockEntity : stockEntityList) {
            stockCount += stockEntity.getCount();
        }

        //添加入库信息头
        EnterstockEntity enterstockEntity = new EnterstockEntity();
        enterstockEntity.setEid(eid);
        enterstockEntity.setHno(stockheadEntity.getHno());
        enterstockEntity.setEnterdate(new Date());
        enterstockEntity.setEremark(stockheadEntity.getRemark());
        //得出入库的总数量
        Integer sumCount = 0;
        for (StockEntity stockEntity : stockheadEntity.getChildren()) {
            sumCount += stockEntity.getCount();
        }
        //入库的数量
        enterstockEntity.setCount(sumCount.toString());
        //采购的数量
        enterstockEntity.setStockcount(stockCount.toString());
        enterstockService.save(enterstockEntity);

        //添加入库信息详情
        for (StockEntity stockEntity : stockheadEntity.getChildren()) {
            EnterstockdetailEntity enterstockdetailEntity = new EnterstockdetailEntity();
            //基于plus自增主键插入返回主键的方法
            enterstockdetailEntity.setEsid(enterstockEntity.getEsid());
            enterstockdetailEntity.setDid(stockEntity.getDid());
            enterstockdetailEntity.setCount(stockEntity.getCount());
            enterstockdetailService.save(enterstockdetailEntity);
        }
        //更改采购订单状态
        StockheadEntity stockhead = new StockheadEntity();
        stockhead.setHid(stockheadEntity.getHid());
        stockhead.setState(StockConstant.StateEnum.STATE_PUT_STORAGE.getState());
        //有就不添加备注
        if (StringUtils.isEmpty(stockheadEntity.getRemark())){
            stockhead.setRemark(stockheadEntity.getRemark());
        }
        stockheadService.updateById(stockhead);


        //添加仓库
        List<WarehouseEntity> warehouseEntities = new ArrayList<>();
        for (StockEntity entity : stockheadEntity.getChildren()) {
            WarehouseEntity warehouseEntity = new WarehouseEntity();
            Integer edate = entity.getEdate();
            Date indate = entity.getIndate();
            warehouseEntity.setMid(entity.getDid());
            warehouseEntity.setCount(entity.getCount());
            warehouseEntity.setIndate(indate);
            warehouseEntity.setEdate(edate);
            warehouseEntity.setSid(entity.getSid());
            warehouseEntity.setCreatedate(new Date());
            //过期日期
            Date date = addDayOfDate(indate, edate);
            warehouseEntity.setOutdate(date);

            warehouseEntities.add(warehouseEntity);
        }
        warehouseService.saveBatch(warehouseEntities);

        return R.ok();
    }

    /**
     * 入库退回  退回不添加库存信息 只修改了订单表状态
     * @param stockheadData
     * @param eid
     * @return
     */
    @PostMapping("/wareNo")
    public R wareNo(@RequestParam String stockheadData,@RequestParam Integer eid){
        StockheadEntity stockheadEntity = JSON.parseObject(stockheadData, StockheadEntity.class);
        //更改采购订单状态
        StockheadEntity stockhead = new StockheadEntity();
        stockhead.setHid(stockheadEntity.getHid());
        stockhead.setState(StockConstant.StateEnum.STATE_SALES_RETURN.getState());
        stockhead.setEid(eid);
        stockhead.setRemark(stockheadEntity.getRemark());

        stockheadService.updateById(stockhead);

        return R.ok();
    }
    /**
     * 在原日期的基础上增加天数
     * @param date
     * @param i
     * @return
     */
    public static Date addDayOfDate(Date date,int i){
        //获得当前地区日历子对象
        Calendar c = Calendar.getInstance();
        //设置日期
        c.setTime(date);
        //添加偏移量
        c.add(Calendar.DATE, i);
        Date newDate = c.getTime();
        return newDate;
    }

}
